/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import bookmaker.HibernateUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author ALIEN
 */
public class BetslipList {
    private List list;
    private Session session;
    private boolean pending;

    /**
     * @return the list
     */
    public List getList() {
        Map map = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
        Object o = map.get("bettor");
        if(o==null) return null;
        long id = ((Bettor)o).getId();
        try {
            SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("select distinct b from Betslip b left join fetch b.bettor as bett where pending="+pending+" and bett.id ="+id);
            list = (ArrayList) q.list();
            tx.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (session != null) {
                session.close();
            }
        }
        return list;
    }

    /**
     * @return the pending
     */
    public boolean isPending() {
        return pending;
    }

    /**
     * @param pending the pending to set
     */
    public void setPending(boolean pending) {
        this.pending = pending;
    }

    public String action(){
        String pend = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("pend").toString();

        pending = (pend.charAt(0)=='y');
        return "history";
    }
}
